package com.example.test.fragment;

import com.example.test.BilldetailActivity;
import com.example.test.CodePayActivity;
import com.example.test.PayBilldetailActivity;
import com.example.test.R;
import com.example.test.demin.TingcheChang;
import com.example.test.demin.User;
import com.example.test.utils.DbDaoUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.DbUtils.DaoConfig;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CodePayFragment extends BaseFragment implements OnClickListener {

	private String result;
	@ViewInject(R.id.tv_ting_name)
	private TextView tv_ting_name;
	@ViewInject(R.id.tv_ting_idname)
	private TextView tv_ting_idname;
	@ViewInject(R.id.tv_ting_tnumber)
	private TextView tv_ting_tnumber;
	@ViewInject(R.id.tv_ting_carnumber)
	private TextView tv_ting_carnumber;
	@ViewInject(R.id.btn_ting_pay)
	private TextView btn_ting_pay;
	@ViewInject(R.id.tv_ting_cost)
	private TextView tv_ting_cost;
	@ViewInject(R.id.rl_not_support)
	private RelativeLayout rl_not_support;
	private TingcheChang tingchechang;
	private DbDaoUtils dao;
	private User user;
	private long ids;
	public CodePayFragment(String result) {
		// TODO Auto-generated constructor stub
		this.result=result;
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
	
			 dao = new DbDaoUtils(ct);
		     tingchechang = dao.findTingcheInfobyTnumber(result);
		     if(tingchechang!=null){
		     tv_ting_idname.setText(tingchechang.getIdname());
		     tv_ting_name.setText(tingchechang.getName());
		     tv_ting_carnumber.setText(tingchechang.getCarnumber());
		     tv_ting_tnumber.setText(result);
		     tv_ting_cost.setText(String.valueOf(tingchechang.getMoney()));
		     }
		     else{
		    	 rl_not_support.setVisibility(View.VISIBLE);
		     }
		     btn_ting_pay.setOnClickListener(this);
		
	}

	@Override
	public View initView(LayoutInflater inflater) {
		View view=inflater.inflate(R.layout.code_pay_layout, null);
		// TODO Auto-generated method stub
		ViewUtils.inject(this,view);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.btn_ting_pay:
			sp=ct.getSharedPreferences("user", Context.MODE_PRIVATE);
			String username=sp.getString("username", "");
			user = dao.findUserInfo(username);
			double yue=user.getMoney()-tingchechang.getMoney();
			if(yue>0){
				dao.updateMoney(username, yue);
				Toast.makeText(ct, "支付成功", 0).show();
				ids = dao.insertBill(tingchechang.getName(), tingchechang.getCarnumber(), tingchechang.getMoney());
				System.out.println(ids);
				Intent intent=new Intent(ct, BilldetailActivity.class);
				intent.putExtra("fromwhat", "pay");
				int ii = (int)ids;  
				intent.putExtra("ids", ii);
				startActivity(intent);
				((CodePayActivity)ct).finish();
				
			}else{
				Toast.makeText(ct, "支付失败", 0).show();
			}
			
			break;

		default:
			break;
		}
	}

}
